export enum RiskLevel {
    lowest = "低风险",
    low = "中低风险",
    medium = "中风险",
    high = "中高风险",
}

export const getLevelByPercent = (percent: number): RiskLevel => {
    if (percent <= 20) {
        return RiskLevel.lowest
    } else if (percent <= 40) {
        return RiskLevel.low
    } else if (percent <= 60) {
        return RiskLevel.medium
    } else {
        return RiskLevel.high
    }
}
export const getLevelColor = (level: RiskLevel): string => {
    switch (level) {
        case RiskLevel.lowest:
            return "#5dc264";
        case RiskLevel.low:
            return "#40b4f3";
        case RiskLevel.medium:
            return "#f9d423";
        case RiskLevel.high:
            return "#ffae43";
    }
}

export interface PredictTag {
    // amber、 blue、 cyan、 green、 grey、 indigo、 light-blue、 light-green、
    // lime、 orange、 pink、 purple、 red、 teal、 violet、 yellow、 white
    key: string,
    content: string,
}

export interface Predict {
    key: string,
    condition: boolean,
    percent: number,
    riskLevel: RiskLevel,
    time: string,
    prompt: string,
    tags: PredictTag[]
}

const KEY_PREDICT_LIST = "predictList"
const KEY_NOW_PREDICT = "nowPredict"
//生成一个对话的ID，确保不会重复
const generateID = (list: Predict[]) => {
    let maxID = 0
    for (const i in list) {
        const nowID = Number(list[i].key.substring("predict".length))
        if (nowID > maxID) {
            maxID = nowID
        }
    }
    return maxID + 1
}

//简单的获取ID，这里只是简单的实现，后面可以再做调整
const getPredictKeyByID = (id: number) => {
    return "predict" + id
}
const generateUniqueKey = () => {
    return getPredictKeyByID(generateID(getPredictList()))
}
export const getFormattedDateWithLocale = (onlyDay: boolean = false): string => {
    const now = new Date();
    const year = now.getFullYear();
    // 月份从 0 开始计数，所以要加 1
    const month = now.getMonth() + 1;
    const day = now.getDate();
    if (onlyDay){
        return `${year}年${month}月${day}日`;
    }
    const hours = now.getHours();
    const minutes = String(now.getMinutes()).padStart(2, '0');
    return `${year}年${month}月${day}日 ${hours}:${minutes}`;
}

export const setNowPredictKey = (key: string) => {
    localStorage.setItem(KEY_NOW_PREDICT, key)
}
export const getNowPredictKey = () => {
    const key = localStorage.getItem(KEY_NOW_PREDICT)
    return key ? key : ""
}
export const getPredictList = (): Predict[] => {
    const list = localStorage.getItem(KEY_PREDICT_LIST)
    if (list) {
        return JSON.parse(list)
    }
    localStorage.setItem(KEY_PREDICT_LIST, "[]")
    return []
}
export const getPredictByKey = (key: string): Predict | null => {
    const list = localStorage.getItem(KEY_PREDICT_LIST)
    let ret = null
    if (list) {
        const json = JSON.parse(list)
        json.map((item: Predict) => {
            if (item.key == key) {
                ret = item
            }
        })
    }
    return ret
}
export const addPredict = (predict: Predict) => {
    predict.key = generateUniqueKey()
    const json = localStorage.getItem(KEY_PREDICT_LIST)
    const list = JSON.parse(json ? json : "[]")
    list.unshift(predict)
    localStorage.setItem(KEY_PREDICT_LIST, JSON.stringify(list))
    return predict.key
}
export const deletePredict = (key: string) => {
    const json = localStorage.getItem(KEY_PREDICT_LIST)
    const list = JSON.parse(json ? json : "[]")
    const index = list.findIndex((predict: Predict) => predict.key === key)
    if (index != -1) {
        list.splice(index, 1)
    }
    localStorage.setItem(KEY_PREDICT_LIST, JSON.stringify(list))
}